package com.eascs.admin.o2o.data;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.common.constant.CacheNameConstant;
import com.eascs.admin.common.service.BaseSevice;
import com.eascs.common.util.StringUtil;
import com.eascs.site.tag.select.SelectDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import java.util.ArrayList;
import java.util.List;

@Service
public class SelectDataServiceImpl extends BaseSevice implements SelectDataService {

	@Autowired
	PubDatadictionaryDao pubDatadictionaryDao;
	@Override
	@Cacheable(CacheNameConstant.SELECT_DATA)
	public List<Object[]> getDataSouce(String dataSouceType, String dataSouceFrom) {
		List<Object[]> dataSouceList = new ArrayList<>();

		if(StringUtil.isNotBlank(dataSouceFrom) && dataSouceFrom.indexOf("CRM")!=-1) {
			List<Object> listParams = new ArrayList<Object>();
			StringBuffer sql        = new StringBuffer("SELECT item.item_code,item.item_name FROM pub_dditem_crm item");
			sql.append(" where item.dd_no = (select dic.dd_no  from 	pub_datadictionary_crm dic  where 1=1");
			if (dataSouceType != null && !"".equals(dataSouceType)) {
				sql.append(" and dd_code= ? )");
				listParams.add(dataSouceType);
			}
			/*if (dataSouceFrom != null && !"".equals(dataSouceFrom)) {
				sql.append(" and parent_code= ?");
				listParams.add(dataSouceFrom);
			}*/
			sql.append(" order by  item_code  asc ");
			dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), listParams.toArray());
			return dataSouceList;
		}

		if(StringUtil.isNotBlank(dataSouceType) &&
				(dataSouceType.equals("province") || dataSouceType.equals("city") || dataSouceType.equals("country"))) {
			List<Object> listParams = new ArrayList<Object>();
			StringBuffer sql        = new StringBuffer("SELECT item.item_code,item.item_name FROM pub_dditem_crm item");
			sql.append(" where item.dd_no = (select dic.dd_no  from 	pub_datadictionary_crm dic  where 1=1");
			if (dataSouceType != null && !"".equals(dataSouceType)) {
				sql.append(" and dd_code= ? )");
				listParams.add(dataSouceType);
			}
			if (dataSouceFrom != null && !"".equals(dataSouceFrom)) {
				sql.append(" and parent_code= ?");
				listParams.add(dataSouceFrom);
			}
			sql.append(" order by  item_code  asc ");
			dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), listParams.toArray());
			return dataSouceList;
		}

		StringBuffer sql = new StringBuffer(
				"select b.item_code,b.item_name from pub_datadictionary a left join pub_dditem b on a.dd_no = b.dd_no  where a.is_lock = 'N' and b.is_lock = 'N'  ");
		if (StringUtil.isNotBlank(dataSouceType)) {
			if (dataSouceType.equals("OTO_LOAN_TYPE_SB")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('BK','FQ','EA','BP','BP_JX','SG','YC','SC') ");
				sql.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), "OTO_LOAN_TYPE");
			} else if (dataSouceType.equals("O2O_LOAN_TYPE_BANK")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('BK','BP','BP_JX','SG','YC','SC') "); // BK BP,只包含终端贷、品牌贷-厂家、品牌贷-经销商
				sql.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), "OTO_LOAN_TYPE");
			}else if (dataSouceType.equals("O2O_LOAN_TYPE_BP")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('BP','BP_JX','SG','YC','SC') "); // BK BP,只包含终端贷、品牌贷-厂家、品牌贷-经销商
				sql.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), "OTO_LOAN_TYPE");
			} else if (dataSouceType.equals("OTOREPAYMENTWAY_SB")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('AKT','RMT') "); // AKT RMT
				sql.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), "OTOREPAYMENTWAY");
			} else if (dataSouceType.equals("BANKTYPEO2O_SB")) {
				sql.append(" and a.dd_code = ?");
				sql.append(" and b.item_code in ('BC','GACZ','EA','CCBHN','QLB','CIBJN','JCB','ZZB','SPDBGZ','SXB','ZLB','SPDBJN') ");
				sql.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), "BANKTYPEO2O");
			} else if (dataSouceType.equals("REGIONALNAME_SB")){
				sql.setLength(0);
				sql.append("select DISTINCT(province_code),province_name from o2o_platform_regional");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString());
			}else{
				sql.append(" and a.dd_code = ?");
				sql.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sql.toString(), dataSouceType);
			}
		}
		// 查询新系统增量部分数据字典
		if (dataSouceList.isEmpty()) {
			StringBuffer sb = new StringBuffer(
					"select b.item_code,b.item_name from pub_datadictionary_o2o a left join pub_dditem_o2o b on a.dd_no = b.dd_no   ");
			if ("ALL".equals(dataSouceFrom)) {
				sb.append(" where 1=1 ");
			}else {
				sb.append(" where a.is_lock = 'N' and b.is_lock = 'N' ");
			}
			if (dataSouceType.equals("SUBJECT_SB")) {
				sb.append(" and a.dd_code = ?");
				sb.append(" and b.item_code in ('BZJ','GLF','FXBCJ') ");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), "SUBJECT");
			} else if (dataSouceType.equals("vocation_HYLB")) {
				sb.append(" and a.dd_code = ?");
				sb.append(" and b.item_code not in ('04') ");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), "vocation");
			} else if(dataSouceType.equals("SYSDEMANDTYPE")){
				sb.append(" and a.dd_code = ?");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), "SYSDEMANDTYPE");
			} else if(dataSouceType.equals("SYSDEMANDINSERTTYPE")){
				sb.append(" and a.dd_code = ? and b.item_code not in ('GNBG','SYSERR','XTZX','XTCW','MBI')");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), "SYSDEMANDTYPE");
			}
			else if(dataSouceType.equals("BAD_LOAN_TYPE_DDC")){
				sb.append(" and a.dd_code = 'BAD_LOAN_TYPE' and b.item_code in ('DC','LXDC')");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString());
			}
			else if(dataSouceType.equals("BAD_LOAN_TYPE_FQEA")){
				sb.append(" and a.dd_code = 'BAD_LOAN_TYPE' and b.item_code in ('FQ','EA')");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString());
			}else if (dataSouceType.equals("SQZT_NS")) {
				sb.append(" and a.dd_code = ?");
				sb.append(" and b.item_code not in ('NEW','Z','ZXSX','O','HN','DC','YQ') ");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), "SQZT");
			}
			else if (dataSouceType.equals("SQZT_MQSB")) {
				sb.append(" and a.dd_code = ?");
				sb.append(" and b.item_code not in ('NEW','Z','ZF','N','ZXSX','O','HN','DC','YQ') ");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), "SQZT");
			}else if (dataSouceType.equals("SQZT_ZX")) {
				sb.append(" and a.dd_code = ?");
				sb.append(" and b.item_code not in ('ZXSX','O','HN','DC','YQ') ");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), "SQZT");
			}
			
			else {
				sb.append(" and a.dd_code = ?");
				sb.append(" order by b.item_order asc ");
				dataSouceList = pubDatadictionaryDao.findObjectsList(sb.toString(), dataSouceType);
			}
		}

		return dataSouceList;
	}

}
